/*
 * Copyright 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mymusic;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;


public class AlbumActivity extends Activity {

	private ListView mainListView;
	private ArrayAdapter<String> listAdapter;
	ListAdapter tracksListAdapter;
	String[] trackData;
	String[] tracks;
	String[] albumIds;
	Track[] albumTracks;
 
	MediaPlayer mMediaPlayer;
	ArrayList<HashMap<String, Track>> albumTracks2 ;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tracks_list);
	
		// Find the ListView resource.
		mainListView = (ListView) findViewById(R.id.mainListView);

		Long albumId = (Long) getIntent().getExtras().get("albumId");
		
		albumTracks = getAudioList(albumId);

		// Create ArrayAdapter using the planet list.
		listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, tracks);
		// Adding menuItems to ListView
		//tracksListAdapter= new  SimpleAdapter(this, albumTracks,R.layout.tracks_list,new String[]{"trackTitle"},new int[] {R.id.mainListView});
		 
		//etListAdapter(tracksListAdapter);
		// Set the ArrayAdapter as the ListView's adapter.
		mainListView.setAdapter(listAdapter);
		//mMediaPlayer =new MediaPlayer();

		/*// selecting single ListView item
		ListView lv = getListView();
		// listening to single listitem click
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// getting listitem index
				//int songIndex = position;
				Long albumId = (Long) getIntent().getExtras().get("albumId");
				 
				Track track=new Track();
				track.setAlbumId(albumId);
				
				Intent intent = new Intent(getApplicationContext(),AlbumArt.class);
				intent.putExtra("position",position);
				intent.putExtra("albumTracks", albumTracks);
				
				startActivity(intent);
			}		 });*/
	 mainListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {
				try {
					//playSong(trackData[position]);
					//Long albumId = (Long) getIntent().getExtras().get("albumId");
					//tracks = getAudioList(albumId);
					//Track track=new Track();
					//track.setAlbumId(albumId);
					
					Intent intent = new Intent(getApplicationContext(),AlbumArt.class);
					intent.putExtra("position",position);
					intent.putExtra("albumTracks", albumTracks);
					
					startActivity(intent);
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} 
				
			}
		}); 
	}

	private Track[] getAudioList(long albumId) {

		String[] projection = new String[] {
				MediaStore.Audio.Media.DISPLAY_NAME,
				MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID,
				MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,
				MediaStore.Audio.Media.TRACK };
	 
		String[] selectionArgs = null;
		String sortOrder = MediaStore.Audio.Media.TRACK + " ASC";
		String where = MediaStore.Audio.Media.ALBUM_ID + "=" + albumId + " AND "
				+ MediaStore.Audio.Media.IS_MUSIC + "=1";
		Cursor mCursor = getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, where,
				selectionArgs, sortOrder);
 
		int count = mCursor.getCount();
		albumTracks= new Track[count];
		/*trackData = new String[count];
		
		albumIds = new String[count];*/
		tracks = new String[count];
		int i = 0;
		if (mCursor.moveToFirst()) {
			do {
			
				Track track = new Track();
				String trackTitle = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
				String trackData = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
				//albumIds[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
				track.setTrackTitle(trackTitle);
				track.setTrackData(trackData);
				track.setAlbumId(albumId);
				//trackMap.put(trackTitle, track);
				tracks[i]=trackTitle;
				albumTracks[i]=track;
				i++;
			} while (mCursor.moveToNext());
		}

		mCursor.close();

		return albumTracks;
	}
	

	 
}
